var adduser = {
    init: function () {
        this.loadData();
        this.loadEvent();
        layui.upload.render({
            elem: '#upload',
            url: URL.INDEX.UPLOADFILE,
            before: function (obj) {
                obj.preview(function (index, file, result) {
                    $('#upload img').attr('src', result);
                });
            },
            done: function (res) {
                if (res.state == 1) {
                    var data = res.data[0];
                    // $('#upload img').attr('src',data.img_host+""+data.file_name);
                    $('#headimg').val(data.id);
                } else {
                    njzxw.alert({
                        error: true,
                        msg: '上传失败'
                    })
                    $('#upload img').attr('src', '');
                }
            }
        });
    },
    initSelectableOrg: null,
    initSelectableRole: null,
    initSelecttableOrgRule: null,
    orgMove: '',
    roleMove: '',
    orgRuleMove: '',
    loadEvent: function () {
        $('#orgMenumenutitle').keyup(function () {
            var val = this.value;
            adduser.initSelectableOrg.treeview('search', [val, {
                attribute: 'text',
                ignoreCase: false,
                exactMatch: false
            }]);
        });
        $('#roleMenumenutitle').keyup(function () {
            var val = this.value;
            adduser.initSelectableRole.treeview('search', [val, {
                attribute: 'text',
                ignoreCase: false,
                exactMatch: false
            }])
        });
        $('#orgruleMenumenutitle').keyup(function () {
            var val = this.value;
            adduser.initSelecttableOrgRule.treeview('search', [val, {
                attribute: 'text',
                ignoreCase: false,
                exactMatch: false
            }])
        });
    },
    /**
     * 保存操作
     * @param obj
     * @param closefn
     * @returns {Boolean}
     */
    save: function () {
        //选择组织
        var orgNode = adduser.initSelectableOrg.treeview('getChecked');
        if (orgNode.length == 0) {
            njzxw.alert({
                error: true,
                msg: '请选择组织'
            })
            return false;
        }
        var orgn = ",";
        for (var i = 0; i < orgNode.length; i++) {
            orgn += orgNode[i].id + ",";
        }
        $('#orgStr').val(orgn);

        //获取角色
        var roleNode = adduser.initSelectableRole.treeview('getChecked');
        if (roleNode.length == 0) {
            njzxw.alert({
                error: true,
                msg: '请选择角色'
            })
            return false;
        }
        var role = ",";
        for (var i = 0; i < roleNode.length; i++) {
            role += roleNode[i].id + ",";
        }
        $('#roleStr').val(role);

        //获取部门管理组织
        var orgManageNode = adduser.initSelecttableOrgRule.treeview('getChecked');
        var orgnManage = ",";
        if (role.indexOf(",2,") >= 0 && orgManageNode.length == 0) {
            njzxw.alert({
                error: true,
                msg: '选择了部门管理员角色则部门管理必选一个。'
            })
            return false;
        }
        for (var i = 0; i < orgManageNode.length; i++) {
            orgnManage += orgManageNode[i].id + ",";
        }
        $('#orgManagerStr').val(orgnManage);


        return njzxw.formData('#form1');
    },
    /**
     * 加载角色跟组织信息
     */
    loadData: function () {
        //加载菜单
        njzxw.ajax({
            url: URL.USER.LOADROLEORG,
            data: {
                id: njzxw.getQueryParamsString('id')
            },
            fun: function (a, b) {
                if (a) {
                    if (b.state == 1) {
                        $('.orgManagediv').hide();
                        console.log("====》", b);
                        var data = b.org;
                        var cnt = data.length;
                        var href = "#";
                        var json = [];
                        for (var i = 0; i < cnt; i++) {
                            var id0 = data[i].id;
                            var org_name0 = data[i].org_name;
                            var supper_id0 = data[i].supper_id;
                            var levels0 = data[i].levels;
                            var da0 = {};
                            if (supper_id0 == 0) {//第一级
                                da0.text = org_name0;
                                da0.id = id0;
                                da0.supper_id = supper_id0;
                                var nodes0 = adduser.loadNextOrg(data, id0);

                                if (nodes0.length != 0) {
                                    da0.nodes = nodes0;
                                } else {
                                    da0.href = '#' + id0;
                                }
                                da0.tags = ['' + nodes0.length + ''];
                                json.push(da0);
                            }
                        }
                        adduser.initSelectableOrg = $('#orgMenu').treeview({
                            color: "#428bca",
                            expandIcon: 'glyphicon glyphicon-chevron-right',
                            collapseIcon: 'glyphicon glyphicon-chevron-down',
                            nodeIcon: 'glyphicon glyphicon-bookmark',
                            showIcon: false,
                            showTags: true,
                            highlightSelected: false,
                            showCheckbox: true,
                            multiSelect: true,
                            levels: 1,
                            data: json,
                            //勾选事件
                            onNodeChecked: function (event, node) {
                                var checkableNodes1 = adduser.initSelectableOrg.treeview('search', [node.id, {
                                    attribute: 'supper_id',
                                    ignoreCase: false,
                                    exactMatch: true
                                }]);
                                adduser.initSelectableOrg.treeview('checkNode', [checkableNodes1, {silent: $('#chk-check-silent').is(':checked')}]);
                                adduser.initSelectableOrg.treeview('clearSearch');
                            },
                            //取消勾选事件
                            onNodeUnchecked: function (event, node) {
                                adduser.initSelectableOrg.treeview('checkNode', [node, {silent: $('#chk-check-silent').is(':checked')}]);
                            },
                            onMoveIn: function (event, node) {
                                //common.log(node);
                                adduser.orgMove = node.id;
                            },
                            onMoveOut: function (event, node) {
                                //common.log(node);
                                adduser.orgMove = '';
                            }
                        });

                        adduser.initSelecttableOrgRule = $('#orgRuleMenu').treeview({
                            color: "#428bca",
                            expandIcon: 'glyphicon glyphicon-chevron-right',
                            collapseIcon: 'glyphicon glyphicon-chevron-down',
                            nodeIcon: 'glyphicon glyphicon-bookmark',
                            showIcon: false,
                            showTags: true,
                            highlightSelected: false,
                            showCheckbox: true,
                            multiSelect: true,
                            levels: 1,
                            data: json,
                            //勾选事件
                            onNodeChecked: function (event, node) {
                                var checkableNodes1 = adduser.initSelecttableOrgRule.treeview('search', [node.id, {
                                    attribute: 'supper_id',
                                    ignoreCase: false,
                                    exactMatch: true
                                }]);
                                adduser.initSelecttableOrgRule.treeview('checkNode', [checkableNodes1, {silent: $('#chk-check-silent').is(':checked')}]);
                                adduser.initSelecttableOrgRule.treeview('clearSearch');
                            },
                            //取消勾选事件
                            onNodeUnchecked: function (event, node) {
                                adduser.initSelecttableOrgRule.treeview('checkNode', [node, {silent: $('#chk-check-silent').is(':checked')}]);
                            },
                            onMoveIn: function (event, node) {
                                //common.log(node);
                                adduser.orgRuleMove = node.id;
                            },
                            onMoveOut: function (event, node) {
                                //common.log(node);
                                adduser.orgRuleMove = '';
                            }
                        });

                        //加载角色信息
                        var dataRole = b.role;
                        var cntRole = dataRole.length;
                        var hrefRole = "#";
                        var jsonRole = [];
                        for (var i = 0; i < cntRole; i++) {//取层级关系 第一级
                            var da = {
                                id: dataRole[i].id,
                                text: dataRole[i].role_name,
                                isdelete: dataRole[i].isdelete
                            };
                            jsonRole.push(da);
                        }

                        adduser.initSelectableRole = $('#roleMenu').treeview({
                            color: "#428bca",
                            expandIcon: 'glyphicon glyphicon-chevron-right',
                            collapseIcon: 'glyphicon glyphicon-chevron-down',
                            nodeIcon: 'glyphicon glyphicon-bookmark',
                            showIcon: false,
                            showTags: true,
                            highlightSelected: false,
                            showCheckbox: true,
                            levels: 1,
                            data: jsonRole,
                            //勾选事件
                            onNodeChecked: function (event, node) {
                                if (node.id == 2) {
                                    $('.orgManagediv').show();
                                }
                            },
                            //取消勾选事件
                            onNodeUnchecked: function (event, node) {
                                if (node.id == 2) {
                                    $('.orgManagediv').hide();
                                    adduser.initSelecttableOrgRule.treeview('uncheckAll', {silent: true});
                                    ;
                                }
                            },
                        });
                        //给对应的组织与角色进行赋值
                        var orgStr = b.user.org_id.split(',');
                        for (var i = 0; i < orgStr.length; i++) {
                            var orgNode = adduser.initSelectableOrg.treeview('search', [orgStr[i], {
                                attribute: 'id',
                                ignoreCase: false,
                                exactMatch: true
                            }]);
                            adduser.initSelectableOrg.treeview('checkNode', [orgNode, {silent: $('#chk-check-silent').is(':checked')}]);
                        }
                        adduser.initSelectableOrg.treeview('clearSearch');

                        //给对应的组织与角色进行赋值
                        if (b.user.org_manage_id != undefined) {
                            var orgManageStr = b.user.org_manage_id.split(',');
                            for (var i = 0; i < orgManageStr.length; i++) {
                                var orgManageNode = adduser.initSelecttableOrgRule.treeview('search', [orgManageStr[i], {
                                    attribute: 'id',
                                    ignoreCase: false,
                                    exactMatch: true
                                }]);
                                adduser.initSelecttableOrgRule.treeview('checkNode', [orgManageNode, {silent: $('#chk-check-silent').is(':checked')}]);
                            }
                            adduser.initSelecttableOrgRule.treeview('clearSearch');
                        }

                        var roleStr = b.user.role_id.split(',');
                        for (var i = 0; i < roleStr.length; i++) {
                            var roleNode = adduser.initSelectableRole.treeview('search', [roleStr[i], {
                                attribute: 'id',
                                ignoreCase: false,
                                exactMatch: true
                            }]);
                            adduser.initSelectableRole.treeview('checkNode', [roleNode, {silent: $('#chk-check-silent').is(':checked')}]);
                        }
                        adduser.initSelectableRole.treeview('clearSearch');
                    } else {
                        njzxw.alert({
                            error: true,
                            msg: "角色跟组织信息加载失败"
                        });
                    }
                } else {
                    njzxw.alert({
                        error: true,
                        msg: "角色跟组织信息加载失败"
                    });
                }
            }
        });
    },
    /**
     * 重汇组织选择器
     */
    loadNextOrg: function (data, id) {
        var cnt = data.length;
        var href = "#";
        var json = [];
        for (var i = 0; i < cnt; i++) {
            var id0 = data[i].id;
            var org_name0 = data[i].org_name;
            var supper_id0 = data[i].supper_id;
            var levels0 = data[i].levels;
            var da0 = {};
            if (supper_id0 == id) {//第一级
                da0.text = org_name0;
                da0.id = id0;
                da0.supper_id = supper_id0;
                var nodes0 = adduser.loadNextOrg(data, id0);

                if (nodes0.length != 0) {
                    da0.nodes = nodes0;
                } else {
                    da0.href = '#' + id0;
                }
                da0.tags = ['' + nodes0.length + ''];
                json.push(da0);
            }
        }
        return json;
    },
    /**
     * table表格参数处理
     * @returns {Array}
     */
    fnTableParam: function () {
        return [{name: 'title', value: $('#title').val()}];
    }
};